<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    div {
      width: 100px;
      height: 100px;
      background-color: aqua;
      position: absolute;
      left: 0;
      top: 0;
    }
  </style>
  <body>
    <div></div>
    <script>
      var oDiv = document.getElementsByTagName('div');
      function newTon(dom, speedX, speedY) {
        clearInterval(dom.timer);
        var g = 3;
        dom.timer = setInterval(() => {
          speedY += g;
          var newTop = dom.offsetTop + speedY;
          var newLeft = dom.offsetLeft + speedX;
          if (
            newTop >
            document.documentElement.clientHeight - dom.clientHeight
          ) {
            speedY = -speedY;
            speedY *= 0.8;
            speedX *= 0.8;
            newTop = document.documentElement.clientHeight - dom.clientHeight;
          }
          if (newTop <= 0) {
            speedY = -speedY;
            speedY *= 0.8;
            speedX *= 0.8;
          }
          if (
            newLeft >
            document.documentElement.clientWidth - dom.clientWidth
          ) {
            speedX = -speedX;
            speedY *= 0.8;
            speedX *= 0.8;
            newTop = document.documentElement.clientWidth - dom.clientWidth;
          }
          if (newLeft <= 0) {
            speedX = -speedY;
            speedX *= 0.8;
            speedX *= 0.8;
          }
          if (Math.abs(speedX) < 1) {
            speedX = 0;
          }
          if (Math.abs(speedY) < 1) {
            speedY = 0;
          }
          if (speedX == 0 && speedY == 0) {
            clearInterval(dom.timer);
          } else {
            dom.style.top = newTop + 'px';
            dom.style.left = newLeft + 'px';
          }
        }, 30);
      }
    </script>
  </body>
</html>
